#include "cgic.h"
#include "glib.h"
#include "MsgUtils.h"
#include "global.h"
int cgiMain()
{
    //1.获取用户的请求，解析出来
    char gatewayIp[128];
    char location[128];

    cgiFormString("gatewayIp",gatewayIp,128);
    cgiFormString("location",location,128);

    //2.写入到指定的配置文件中去
    //读取配置文件
    GKeyFile *configiniPath = g_key_file_new();
    if(!g_key_file_load_from_file(configiniPath,"./config.ini",G_KEY_FILE_NONE,NULL))
    {
        g_warning("load config.ini error!");
        return -1;
    }
    gchar * path = g_key_file_get_string(configiniPath,"config","devinfo_path",NULL);
    g_key_file_free(configiniPath);
    GKeyFile * configPath = g_key_file_new();
    g_key_file_set_string(configPath,"config","gatwayIp",gatewayIp);
    g_key_file_set_string(configPath,"config","location",location);
    g_key_file_save_to_file(configPath,path,NULL);
    g_key_file_free(configPath);

    //3.通知心跳进程，读取数据发请求
    //3.1获取队列
    int mqId = getMsgQueue(PATHNAME_BOA_MAIN,PROCESS_ID_BOA_MAIN);
    if (mqId == -1)
    {
        perror("create msq error");
        return -1;
    }
    Message msg={
        .type = 1,
        .text = "data1"
    };  
    sendMsg(mqId,&msg,strlen(msg.text)+1);

    //4.给用户一些响应
    cgiHeaderContentType("text/html");
	/* Top of the page */ 
	fprintf(cgiOut, "<HTML><HEAD> <meta charset='UTF-8'>\n");
	fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H1>%s</H1>\n","success");
    fprintf(cgiOut, "</BODY></HTML>\n");
}